//
//  ChuanDishViewController.m
//  Delicious
//
//  Created by gy on 16/5/5.
//  Copyright © 2016年 gy. All rights reserved.
//

#import "ChuanDishViewController.h"
@interface ChuanDishViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *TableView;
@property (nonatomic, strong) NSMutableArray *allArr;
@end

@implementation ChuanDishViewController
- (NSMutableArray *)allArr
{
    if (!_allArr) {
        _allArr = [NSMutableArray array];
    }
    return _allArr;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(Click)];
    self.navigationItem.leftBarButtonItem = leftItem;
    self.TableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];
    self.TableView.dataSource = self;
    self.TableView.delegate = self;
    
    [self.TableView registerNib:[UINib nibWithNibName:@"ChuanDishTableViewCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:self.TableView];
    
    [self getData];
    
    
}

- (void)getData
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [manager GET:ChuanDishURL parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dictonary = responseObject;
        NSArray *array = dictonary[@"data"];
        for (NSDictionary *dict in array) {
            ChuanDishModel *model = [[ChuanDishModel alloc] init];
            [model setValuesForKeysWithDictionary:dict];
            [self.allArr addObject:model];
        }
        [self.TableView reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

   }];
    
}

- (void)Click{
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    drawerViewController *drawer = [delegate.VCDic objectForKey:@"drawer"];
    
    delegate.window.rootViewController = drawer;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.allArr.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ChuanDishTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    ChuanDishModel *model = [self.allArr objectAtIndex:indexPath.row];
    [cell.ImageView sd_setImageWithURL:[NSURL URLWithString:model.imageUrl]];
    [cell setModel:model];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ChuanDishModel *model = [self.allArr objectAtIndex:indexPath.row];
    DetailViewController *detailVC = [[DetailViewController alloc] init];
    detailVC.ID = model.ID;
    [self.navigationController pushViewController:detailVC animated:YES];
    
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
